package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * Search for a specific book copy by name, author, subject of free description
 * @author Benny
 *
 */
public class SearchBookGUI extends JPanel {
	private JTextField textField;
	@SuppressWarnings("rawtypes")
	private JComboBox searchAttributeComboBox;
	public MainFrame mf;
    public DefaultTableModel searchTableModel;
    private JScrollPane scrollPane;
    public JTable table;
    public DefaultTableModel searchTableModel1;
    private JScrollPane scrollPane1;
    public JTable table1;
    public JButton btnNewButton;
    /**
	 * initialize all the text fields of the GUI
	 */
	public void init(){
		textField.setText("");
		searchAttributeComboBox.setSelectedIndex(0);
	}
	/**
	 * Create the panel.
	 *  @param M Instance of the main Frame of the program 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SearchBookGUI(MainFrame M) {
		setLayout(null);
		mf=M;
		JLabel lblSearchBook = new JLabel("Search Book");
		lblSearchBook.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblSearchBook.setBounds(235, 12, 137, 14);
		add(lblSearchBook);
		
		JLabel lblSearchKeyword = new JLabel("Search Keyword:");
		lblSearchKeyword.setBounds(45, 36, 105, 14);
		add(lblSearchKeyword);
		
		textField = new JTextField();
		textField.setBounds(160, 33, 193, 20);
		add(textField);
		textField.setColumns(10);
		
		JLabel lblSearchAttribute = new JLabel("Search By:");
		lblSearchAttribute.setBounds(45, 72, 105, 14);
		add(lblSearchAttribute);
		
		String[] searchOptions = { "Book Name", "Author", "Subject", "Free Description"};
	    searchAttributeComboBox = new JComboBox(searchOptions);
		searchAttributeComboBox.setBounds(160, 69, 187, 20);
		add(searchAttributeComboBox);
		
		searchTableModel= new DefaultTableModel();
        searchTableModel.addColumn("#");
        searchTableModel.addColumn("Book name");
        searchTableModel.addColumn("Author");
        searchTableModel.addColumn("Shelf Place");
        
        scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 98, 580, 196);
        add(scrollPane);
        
        table= new JTable(searchTableModel){
  
                
        public boolean isCellEditable(int rowIndex, int colIndex)
        {
        	return false; //Disallow the editing of any cell
        }};
        
        table.addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent e) {
        		if (e.getClickCount() == 2) 
        		{
        	      JTable target = (JTable)e.getSource();
        	      int row = target.getSelectedRow();
        	      String rc= (String)target.getValueAt(row, 0);
        	      int bookID=Integer.decode(rc);
        	      ArrayList<Object> send = new ArrayList<>();
        	      send.add("SearchCopyID");
        	      send.add(bookID);
        	      mf.client.accept(send);
        	    }
        	}
        });
                  
                          
        
                
        
        scrollPane.setViewportView(table);
        table.setRowSelectionAllowed(true);
        table.setVisible(true);
        
        searchTableModel1= new DefaultTableModel();
        searchTableModel1.addColumn("CopyID");
        searchTableModel1.addColumn("#ofOrders");

        
        scrollPane1 = new JScrollPane();
        scrollPane1.setBounds(144, 306, 314, 166);
        add(scrollPane1);
        
        table1= new JTable(searchTableModel1){
                
                public boolean isCellEditable(int rowIndex, int colIndex) {
                          return false; //Disallow the editing of any cell
                          }
                          };
                
        
        scrollPane1.setViewportView(table1);
        table1.setRowSelectionAllowed(true);
        table1.setVisible(true);
        
        
        
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 for (int i = searchTableModel.getRowCount() - 1; i >= 0; i--) 
                     searchTableModel.removeRow(i);
                        
                 ArrayList<Object> send = new ArrayList<Object>();
                 send.add("BookSearch");
                 send.add(searchAttributeComboBox.getSelectedItem().toString()); /*Search by option */
                 send.add(textField.getText());
                 mf.client.accept(send);
			}
		});
		btnSearch.setBounds(369, 47, 89, 23);
		add(btnSearch);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(481, 47, 89, 23);
		add(btnClear);
		
		btnNewButton = new JButton("Return");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				init();
				setVisible(false);
				mf.lg.setVisible(true);
				mf.lg.btnLogin.setEnabled(true);
			}
		});
		btnNewButton.setBounds(495, 464, 95, 23);
		btnNewButton.setVisible(false);
		add(btnNewButton);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		
		
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(715, 597);
		add(picLabel);

	}
}
